<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","nik");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_stafflist.php";
$FormName            = GetParam("FormName","");
if ((GetParam("FormName","")=="MemorialTransForm") && !GetParam("SendToBack","")) $FileTemplate  = "ais_transaction_stafflist_memotrans.html";
elseif ((GetParam("FormName","")=="CashBankForm") && !GetParam("SendToBack","")) {$FileTemplate = "ais_transaction_stafflist_cashbanktrans.html";}
elseif ((GetParam("FormName","")=="CashBankPaymentForm") && !GetParam("SendToBack","")) {$FileTemplate = "ais_transaction_stafflist_cashbanktrans_pay.html";}
else $FileTemplate  = "ais_transaction_stafflist_list.html";

$SubHeaderTitle = "Staff Lookup";
$PageSize      = 10 ;
$HTMLGridList  = "StaffGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$FNama = GetParam("FNama","");
$FCode = GetParam("FCode","");
$FDept = GetParam("deptSearch","");
$need_another_var = GetParam("need_another_var","");
$no_urut = GetParam("no_urut","");


//******* DATABASE FORM QUERY ***********
$SQL = "SELECT *,trim(nik) as nik, tbldept.deptnam as deptnam 
        FROM $DBGeneral.tblstaff LEFT JOIN tbldept ON $DBGeneral.tblstaff.kddept=tbldept.kddept ";
        
if ($FNama)
    if (strlen($WHERE)) $WHERE .= " AND "." nama like '%".$FNama."%'";
    else $WHERE .= " nama like '%".$FNama."%'";
    
if ($FCode)
    if (strlen($WHERE)) $WHERE .= " AND "." nik like '%".$FCode."%'";
    else $WHERE .= " nik like '%".$FCode."%'";
    
if ($FDept)
    if (strlen($WHERE)) $WHERE .= " AND "." tblstaff.kddept like '%".$FDept."%'";
    else $WHERE .= " tblstaff.kddept like '%".$FDept."%'";

    if (strlen($WHERE)) $WHERE = " WHERE ".$WHERE;
    else $WHERE = " WHERE 1";

$SQL = $SQL.$WHERE." ORDER BY nik";




$WHERE = "";

$SQLCount = "SELECT count(*)
        FROM $DBGeneral.tblstaff LEFT JOIN tbldept ON $DBGeneral.tblstaff.kddept=tbldept.kddept ";


if ($FNama)
    if (strlen($WHERE)) $WHERE .= " AND "." nama like '%".$FNama."%'";
    else $WHERE .= " nama like '%".$FNama."%'";
    
if ($FCode)
    if (strlen($WHERE)) $WHERE .= " AND "." nik like '%".$FCode."%'";
    else $WHERE .= " nik like '%".$FCode."%'";
    
if ($FDept)
    if (strlen($WHERE)) $WHERE .= " AND "." tblstaff.kddept like '%".$FDept."%'";
    else $WHERE .= " tblstaff.kddept like '%".$FDept."%'";

    if (strlen($WHERE)) $WHERE = " WHERE ".$WHERE;
    else $WHERE = " WHERE 1";

$SQLCount = $SQLCount.$WHERE." ORDER BY nik";




//Select Department
$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("tblstaff",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

?>
